package com.jdone.compus.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.jdone.compus.enums.ChannelType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.data.annotation.LastModifiedDate;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Data
@Entity
@Table(name = "blow_devices")
@ApiModel(value = "吹风机设备记录实体类")
public class BlowDevice {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ApiModelProperty(value = "设备名称")
    @Column(length = 128)
    private String name;

    @ApiModelProperty("所属 IMEI ")
    @Column(name = "imei_record_id",  length = 64)
    private String imei;

    @ApiModelProperty("生产厂家")
    @Column(length = 128)
    private String manufacturer;

    @ApiModelProperty("通道类型（LEFT/RIGHT）")
    @Enumerated(EnumType.STRING)
    @Column(length = 10)
    private ChannelType channel;

    @ApiModelProperty(value = "设备类型 1-插板 2-吹风机")
    private Integer deviceType;

    @ApiModelProperty(value = "设备的详细位置")
    @Column(length = 256)
    private String location;

    @ApiModelProperty(value = "设备状态")
    @Enumerated(EnumType.STRING)
    private DeviceStatus status;

    @ApiModelProperty(value = "创建时间")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;

    @ApiModelProperty(value = "更新时间")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    @ApiModelProperty(value = "二维码")
    @Lob
    private String qrCodeUrl;

    @ApiModelProperty(value = "图片路径")
    @Column(name = "relative_path")
    private String images;

    @ApiModelProperty(value = "使用时间段（示例：08:00-12:00|15:00-19:00）", required = false)
    @Column(name = "use_time", length = 256)
    private String useTime;

    @ApiModelProperty(value = "是否删除 0-表述数据有效 1-数据已删除")
    private Integer isDeleted;


    public enum DeviceStatus {
        NORMAL, DAMAGED, OFFLINE
    }

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
        this.createdAt = now;
        this.updatedAt = now;
        this.status = DeviceStatus.NORMAL;
        this.isDeleted = 0;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updatedAt = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
    }
}
